
#include "mmgr/include/mmgr_modem.h"
#include "misc/version.h"
#include "task_gnss.h"
#include "gnss_power_mgr.h"
#include "misc/hwmisc.h"
#include "misc/misc_system.h"

#include "drivers/modem/qlmodem/task_modem.h"
#include "mmgr/include/mmgr_pdp.h"
#include "mmgr/include/mmgr_misc.h"


#include "service/include/swei_login.h"
#include "service/include/swei_active.h"

#include "lettershell/shell.h"

int32_t cmd_dev_probe(void)
{
  
    /* modem ver */
    char mdm_ver[20] = {0};
    sprintf(mdm_ver, "%s", mmgr_version_get());
    printf("MDM_VER:[%s] ", mdm_ver);

    /* imei */
    char imei[16] = {0};
    sprintf(imei, "%s", mmgr_imei_get());
    printf("IMEI:[%s] ", imei);
    
    /* iot ver */
    unsigned char iot_ver[3]; 
    iot_ver[0] = atoi(BRANCH_VERSION + 1);
    iot_ver[1] = atoi(MAJOR_VERSION + 1);
    iot_ver[2] = atoi(MINOR_VERSION + 1);
    printf("IOT VER:[R%02dA%02dV%02d] ", iot_ver[0], iot_ver[1], iot_ver[2]);

    /* gps_ver */
    char gps_ver[GNSS_FW_VER_LEN + 1] = {0};
    qltask_gnss_firmware_version(gps_ver, sizeof(gps_ver));

    int i = strlen(gps_ver);
    while( --i >= 0) {
        if(gps_ver[i] == '.') {
            gps_ver[i] = 0;
            break;
        }
    }

    printf("GNSS VER:[%s] ", gps_ver);

    /* iot_hw */
    int hw_num = get_hw_verno();
    unsigned char iot_hw[2] = {0};
    iot_hw[0] = hw_num / 100;
    iot_hw[1] = hw_num % 100;
    printf("HW VER:[%d.%02d]\r\n", iot_hw[0], iot_hw[1]);


    /* sim valid */
    bool sim_valid = mmgr_simcard_exist_get();
    printf("SIM valid:[%d] ", sim_valid);

    /* net_status */
    int net_status = mmgr_pdp_status_get();
    printf("PDP status:[%d] ", net_status);

    /* csq */
    /* csq(1~31) already transformed*/
    int rssi;
    modem_get_signal_quality(&rssi, NULL);
    int csq = rssi; 
    printf("CSQ:[%d] ", csq);

    /* rat_state */
    int rat_state;
    int rat_report;
    modem_get_netreg_status(NULL, &rat_state);
    if (rat_state == NET_AUTO) {
        rat_report = 0;   //auto
    } else if (rat_state == NET_2G) {
        rat_report = 1;   //2G
    } else if ( (rat_state == NET_4G) || (rat_state == NET_EMTC) || (rat_state == NET_NBIoT) ) {
        rat_report = 2;   //4G
    } else {
        rat_report = 3;   //others
    }
    printf("rat_state:[%d], report[%d]\r\n ", rat_state, rat_report);


    struct fix_info gnss_info; 
    struct sat_info gnss_sat;
    unsigned int count;
    
    get_last_gnss_info(&gnss_info);
    get_last_sat_info(&gnss_sat, &count);

    /* loc_status */
    bool loc_status = (gnss_info.state == FIX_OK) ? true : false;
    printf("loc_status:[%d] ", loc_status);

    /* gnss_num */
    int gnss_num = gnss_sat.count;
    printf("gnss_num:[%d] ", gnss_num);

    printf("gnss_cn:");
    int gnss_cn[5];
    memset(gnss_cn, 0, 5);

    int gnss_cout_min = gnss_sat.count < 5 ? gnss_sat.count : 5;
    
    for(int i = 0; i < gnss_cout_min; i++) {
        gnss_cn[i] = gnss_sat.sat[i].signal_strength;
        printf("%d:[%d] ", i+1, gnss_cn[i]);   
    }
    printf("\r\n");

    /* plat_conn_status */
    bool plat_conn_status = swei_login_is_ok();
    printf("plat_conn:[%d] ", plat_conn_status);

    /* active_status */
    bool active_status = (ACTIVE == misc_get_active_state() ? true : false);
    printf("dev activated:[%d]\r\n", active_status);

    /* iccid */
    char iccid[21] = {0};
    strncpy((void *)iccid, mmgr_iccid_get(), sizeof(iccid));
    printf("iccid:[%s] ", iccid);

    /* imsi */
    char imsi[16] = {0};
    strncpy((void *)imsi, mmgr_imsi_get(), sizeof(imsi));
    printf("imsi:[%s]\r\n", imsi);
    

    return 0;
}

SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC) | SHELL_CMD_DISABLE_RETURN,
                 device, cmd_dev_probe, dev info pro);
